//
//  MYGChatPositionCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/14.
//

import UIKit
import SVGKit

class MYGChatPositionCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChatPositionModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            reloadLayout()
            if model.isLeft == false {
                self.avatar.sd_setImage(with: URL(string: MYGUserManager.user?.doctor.avatar), placeholderImage: UIImage(named: "resum_head"))
            }else {
                if model.avatar.contains("http") {
                    avatar.sd_setImage(with: URL(string: model.avatar))
                } else {
                    avatar.image = UIImage(named: "head")
                }
            }
//            if model.avatar.contains("http") {
//                avatar.sd_setImage(with: URL(string: model.avatar))
//            } else {
//                if let data = UserDefaults.standard.data(forKey: model.avatar) {
//                    guard let svgImage = SVGKImage(data: data) else {
//                        return
//                    }
//                    
//                    self.avatar.image = svgImage.uiImage
//                } else {
//                    DispatchQueue.global().async {
//                        let url = URL(string: model.avatar)!
//                        guard let data = try? Data(contentsOf: url) else {
//                            return
//                        }
//                        
//                        UserDefaults.standard.setValue(data, forKey: model.avatar)
//                        UserDefaults.standard.synchronize()
//                        guard let svgImage = SVGKImage(data: data) else {
//                            return
//                        }
//                
//                        DispatchQueue.main.async {
//                            self.avatar.image = svgImage.uiImage
//                        }
//                    }
//                }
//            }
//            if !model.isLeft {
//                self.avatar.sd_setImage(with: URL(string: MYGUserManager.user?.doctor.avatar), placeholderImage: UIImage(named: "resum_head"))
//            }
            nameLabel.text = model.name
            positionNameLabel.text = model.posisitonName
            hintLabel.text = model.hint
            contentImageView.sd_setImage(with: URL(string: model.imageUrl))
        }
    }

    var reloadAction: (()->Void)?
    var withdrawAction: (()->Void)?
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(mainView)
        mainView.addSubview(positionNameLabel)
        mainView.addSubview(hintLabel)
        mainView.addSubview(contentImageView)
    
        reloadLayout()
    }
    
    private func reloadLayout() {
        guard let isLeft = model?.isLeft else {
            return
        }
        
        avatar.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalToSuperview().offset(25)
            } else {
                make.right.equalToSuperview().inset(25)
            }
            make.top.equalToSuperview().offset(20)
            make.width.height.equalTo(36)
        }
        
        nameLabel.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalTo(avatar.snp.right).offset(10)
            } else {
                make.right.equalTo(avatar.snp.left).offset(-10)
            }
            make.top.equalTo(avatar.snp.top)
        }
        
        mainView.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalTo(avatar.snp.right).offset(10)
            } else {
                make.right.equalTo(avatar.snp.left).offset(-10)
            }
            
            make.top.equalTo(nameLabel.snp.bottom).offset(7)
            make.width.equalTo(228)
            make.height.equalTo(165)
            make.bottom.equalToSuperview().inset(5)
        }
        
        positionNameLabel.snp.remakeConstraints { make in
            make.left.right.equalToSuperview().inset(14)
            make.top.equalToSuperview().offset(10)
        }
        
        hintLabel.snp.remakeConstraints { make in
            make.left.right.equalToSuperview().inset(14)
            make.top.equalTo(positionNameLabel.snp.bottom).offset(2)
        }
        
        contentImageView.snp.remakeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(103)
        }
        
        nameLabel.setContentHuggingPriority(.required, for: .vertical)
        nameLabel.setContentCompressionResistancePriority(.required, for: .vertical)
    }
        
    // MARK: - lazy load
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 18
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(38, 38, 40, 1)
        label.text = "系统"
        return label
    }()
    
    private(set) lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(247, 247, 247, 1)
        
        let tap = UILongPressGestureRecognizer(target: self, action: #selector(showBack))
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var positionNameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(51, 51, 51, 1)
        return label
    }()
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(121, 121, 121, 1)
        return label
    }()
    
    private lazy var contentImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFit
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(toImage))
        imgV.addGestureRecognizer(tap)
        imgV.isUserInteractionEnabled = true
        return imgV
    }()
}

extension MYGChatPositionCell {
    @objc
    private func toImage()  {
        guard let model = model else {
            return
        }
        let vc = MYGMapPositionController(coor: model.coor)
        UIViewController.currentViewController?.navigationController?.pushViewController(vc)
    }
    
    @objc
    private func showBack() {
        self.withdrawAction?()
    }
}
